<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 两个非重叠子数组的最大和 -->
    <script>
      let maxSumTwoNoOverlap = function (nums, firstLen, secondLen) {
        const presum = [0]
        for (let num of nums) {
          presum.push(presum[presum.length - 1] + num)
        }
        let ans = -1
        for (let i = firstLen, temp = 0, len = nums.length; i + secondLen <= len; i++) {
          // i之前的是firstLen个数, i之后的是secondLen个数
          temp = Math.max(temp, presum[i] - presum[i - firstLen])
          ans = Math.max(ans, temp + presum[i + secondLen] - presum[i])
        }
        for (let i = secondLen, temp = 0, len = nums.length; i + firstLen <= len; i++) {
          // i之前的是secondLen个数, i之后的是firstLen个数
          temp = Math.max(temp, presum[i] - presum[i - secondLen])
          ans = Math.max(ans, temp + presum[i + firstLen] - presum[i])
        }
        return ans
      }
      console.log(maxSumTwoNoOverlap([0, 6, 5, 2, 2, 5, 1, 9, 4], 1, 2))
    </script>
  </body>
</html>
